#include <stdio.h>
#include <stdlib.h>

#define MAX_PRIO 10000
#define BITS_PER_LONG 32
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

#define BIT(nr)            (1UL << (nr))
#define BIT_MASK(nr)        (1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr)        ((nr) / BITS_PER_LONG)
#define BITS_PER_BYTE        8

#define BITS_TO_LONGS(nr)    DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))

#define DECLARE_BITMAP(name,bits) \
	unsigned long name[BITS_TO_LONGS(bits)]

static inline void __set_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

	*p  |= mask;
}

static inline void __clear_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

	*p &= ~mask;
}

static inline void __change_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

	*p ^= mask;
}

static inline int __test_and_set_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
	unsigned long old = *p;

	*p = old | mask;
	return (old & mask) != 0;
}

static inline int __test_and_clear_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
	unsigned long old = *p;

	*p = old & ~mask;
	return (old & mask) != 0;
}

static inline int __test_and_change_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
	unsigned long old = *p;

	*p = old ^ mask;
	return (old & mask) != 0;
}

static inline int test_bit(int nr, const volatile unsigned long *addr)
{
	return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
}

#ifndef container_of
/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:    the type of the container struct this is embedded in.
 * @member:    the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({            \
		const typeof(((type *)0)->member) * __mptr = (ptr);    \
		(type *)((char *)__mptr - offsetof(type, member)); })
#endif

#define list_entry(ptr, type, member) container_of(ptr, type, member)

int main(int argc, char* argv[])
{
	DECLARE_BITMAP(bitmap1, MAX_PRIO);
	DECLARE_BITMAP(bitmap2, MAX_PRIO);
	int i;

	/* 00000000 - 99999999 使用一个bitmap */
	int num1 = 9999;
	int num2 = 9999;

	char buff[32] = {0};

	for (i = 0; i < MAX_PRIO; i++) {

		__clear_bit(i, bitmap1);
		__clear_bit(i, bitmap2);
	}

	__set_bit(num1, bitmap1);
	__set_bit(num2, bitmap2);

	printf("bitmap size = %ld\n", sizeof(bitmap1)/sizeof(long));
	printf("bitmap = %d\n", test_bit(num1, bitmap1));
	printf("bitmap = %d\n", test_bit(num2, bitmap2));
	printf("atoi : %d\n", atoi("0010"));
	printf("%.4d%.4d\n", num1, num2);
	return 0;
}
